<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-card>
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限字符" prop="roleKey">
        <el-input
          v-model="queryParams.roleKey"
          placeholder="请输入权限字符"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-card>
    </el-form>

    <el-card style="margin-top:10px;">
        <el-table v-loading="loading" :data="roleList" border>
        <el-table-column label="角色编号" prop="roleId" width="100" align="center" />
        <el-table-column label="角色名称" prop="roleName"/>
        <el-table-column label="权限字符" prop="roleKey"/>
        <el-table-column label="显示顺序" prop="roleSort" align="center" width="100" />
        <el-table-column label="状态" align="center" width="100">
            <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
            ></el-switch>
            </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
            <template slot-scope="scope">
                <el-dropdown :hide-on-click="false" v-if="scope.row.roleId !== 1">
                    <span class="el-dropdown-link" style="color:blue;">
                        更多操作<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="handleView(scope.row)"><i class="el-icon-document"></i>角色详情</el-dropdown-item>
                        <el-dropdown-item style="color:blue;" @click.native="handleRoleDatasets(scope.row)"><i class="el-icon-view"></i>数据权限</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </template>
        </el-table-column>
        </el-table>

        <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        />

    </el-card>

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
        <el-card style="margin-top:-30px">
            <div slot="header" class="clearfix">
                <span style="font-weight:bold;font-size:18px;color:#329dff;">角色信息</span>
            </div>
            <el-descriptions :column="2" border>
                <el-descriptions-item label="角色名称" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
                    {{ roleDetail.roleName }}
                </el-descriptions-item>
                <el-descriptions-item label="角色标识" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
                    {{ roleDetail.roleKey }}
                </el-descriptions-item>
                <el-descriptions-item label="创建时间" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
                    {{ parseTime(roleDetail.createTime) }}
                </el-descriptions-item>
                <el-descriptions-item label="状态" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="roleDetail.status"/>
                </el-descriptions-item>
            </el-descriptions>
        </el-card>

        <el-card style="margin-top:10px" v-if="roleAuth==false">
            <div slot="header" class="clearfix">
                <span style="font-weight:bold;font-size:18px;color:#329dff;">数据权限</span>
            </div>
            <el-tabs v-model="activeNameView" @tab-click="handleClickView">
                <el-tab-pane label="项目权限" name="vfirst">
                    <el-table :data="projectList" border ref="tablep1">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablep" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="工程权限" name="vsecond">
                    <el-table :data="engineerList" border ref="tablee1">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablee" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="子工程权限" name="vthird">
                    <el-table :data="subEngineerList" border ref="tablese1">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablese" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="站点权限" name="vfourth">
                    <el-table :data="stationList" border ref="tables1">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectables" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </el-card>

        <el-card style="margin-top:10px" v-if="roleAuth==true">
            <div slot="header" class="clearfix">
                <span style="font-weight:bold;font-size:18px;color:#329dff;">数据权限</span>
            </div>
            <el-tabs v-model="activeNameAuth" @tab-click="handleClickAuth">
                <el-tab-pane label="项目权限" name="afirst">
                    <el-table :data="projectList" border @selection-change="handleSelectionChangeP" :row-key="getRowKeysP" ref="tablep">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablep" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                        <el-table-column label="操作" align="center" width="160">
                            <template slot-scope="scope">
                                <el-button v-if="scope.row.hasRole==1" type="text" @click="unhookRoleProject(scope.row)">解除权限</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <center>
                        <el-button type="primary" style="margin-top:20px" @click="saveRoleProjects">保存项目权限</el-button>
                    </center>
                </el-tab-pane>
                <el-tab-pane label="工程权限" name="asecond">
                    <el-table :data="engineerList" border @selection-change="handleSelectionChangeE" :row-key="getRowKeysE" ref="tablee">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablee" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                        <el-table-column label="操作" align="center" width="160">
                            <template slot-scope="scope">
                                <el-button v-if="scope.row.hasRole==1" type="text" @click="unhookRoleEngineer(scope.row)">解除权限</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <center>
                        <el-button type="primary" style="margin-top:20px" @click="saveRoleEngineer">保存工程权限</el-button>
                    </center>
                </el-tab-pane>
                <el-tab-pane label="子工程权限" name="athird">
                    <el-table :data="subEngineerList" border @selection-change="handleSelectionChangeSe" :row-key="getRowKeysSe" ref="tablese">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectablese" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                        <el-table-column label="操作" align="center" width="160">
                            <template slot-scope="scope">
                                <el-button v-if="scope.row.hasRole==1" type="text" @click="unhookRoleSubEngineer(scope.row)">解除权限</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <center>
                        <el-button type="primary" style="margin-top:20px" @click="saveRoleSubEngineer">保存子工程权限</el-button>
                    </center>
                </el-tab-pane>
                <el-tab-pane label="站点权限" name="afourth">
                    <el-table :data="stationList" border @selection-change="handleSelectionChangeS" :row-key="getRowKeysS" ref="tables">
                        <el-table-column type="selection" width="55" align="center" :selectable="selectables" />
                        <el-table-column label="序号" align="center" type="index" width="55" />
                        <el-table-column label="名称" align="center" prop="name" />
                        <el-table-column label="编码" align="center" prop="code" />
                        <el-table-column label="操作" align="center" width="160">
                            <template slot-scope="scope">
                                <el-button v-if="scope.row.hasRole==1" type="text" @click="unhookRoleStation(scope.row)">解除权限</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <center>
                        <el-button type="primary" style="margin-top:20px" @click="saveRoleStation">保存站点权限</el-button>
                    </center>
                </el-tab-pane>
            </el-tabs>
        </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { listRole } from "@/api/system/role";
import { getRoleProjectList,saveRoleProjects,unhookRoleProject,getBindRoleProjectList } from "@/api/system/project";
import { getRoleEngineerList,saveRoleEngineer,unhookRoleEngineer,getBindRoleEngineerList } from "@/api/system/engineer";
import { getRoleSubEngineerList,saveRoleSubEngineer,unhookRoleSubEngineer,getBindRoleSubEngineerList } from "@/api/system/subengineer";
import { getRoleStationList,saveRoleStation,unhookRoleStation,getBindRoleStationList } from "@/api/system/station";

export default {
  name: "RoleAuth",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      projectList: [],
      engineerList: [],
      subEngineerList: [],
      stationList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      idsp: [],
      // 非单个禁用
      singlep: true,
      // 非多个禁用
      multiplep: true,
      // 选中数组
      idse: [],
      // 非单个禁用
      singlee: true,
      // 非多个禁用
      multiplee: true,
      // 选中数组
      idsse: [],
      // 非单个禁用
      singlese: true,
      // 非多个禁用
      multiplese: true,
      // 选中数组
      idss: [],
      // 非单个禁用
      singles: true,
      // 非多个禁用
      multiples: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      activeNameAuth: "afirst",
      activeNameView: "vfirst",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      roleAuth: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined
      },
      roleDetail: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClickView(tab, event){
        if(tab.index==1){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getBindRoleEngineerList(query).then(res => {
                this.engineerList = [];
                this.engineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.engineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablee1.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==2){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getBindRoleSubEngineerList(query).then(res => {
                this.subEngineerList = [];
                this.subEngineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.subEngineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablese1.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==3){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getBindRoleStationList(query).then(res => {
                this.stationList = [];
                this.stationList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.stationList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tables1.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==0){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getBindRoleProjectList(query).then(res => {
                this.projectList = [];
                this.projectList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.projectList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablep1.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }
    },
    handleClickAuth(tab, event){
        if(tab.index==1){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleEngineerList(query).then(res => {
                this.engineerList = [];
                this.engineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.engineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablee.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==2){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleSubEngineerList(query).then(res => {
                this.subEngineerList = [];
                this.subEngineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.subEngineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablese.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==3){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleStationList(query).then(res => {
                this.stationList = [];
                this.stationList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.stationList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tables.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }else if(tab.index==0){
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleProjectList(query).then(res => {
                this.projectList = [];
                this.projectList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.projectList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablep.toggleRowSelection(v, true);
                    }
                });
                });
            })
        }
    },
    /** 查询角色列表 */
    getList() {
        this.loading = true;
        listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChangeP(selection) {
      this.idsp = selection.map(item => item.uuid)
      this.singlep = selection.length!=1
      this.multiplep = !selection.length
    },
    selectablep(row, index) {
      if (row.hasRole == 1) {
        return false; // 禁用
      } else {
        return true; //不禁用
      }
    },
    getRowKeysP(row) {
      return row.uuid;
    },
    // 多选框选中数据
    handleSelectionChangeE(selection) {
      this.idse = selection.map(item => item.uuid)
      this.singlee = selection.length!=1
      this.multiplee = !selection.length
    },
    selectablee(row, index) {
      if (row.hasRole == 1) {
        return false; // 禁用
      } else {
        return true; //不禁用
      }
    },
    getRowKeysE(row) {
      return row.uuid;
    },
    // 多选框选中数据
    handleSelectionChangeSe(selection) {
      this.idsse = selection.map(item => item.uuid)
      this.singlese = selection.length!=1
      this.multiplese = !selection.length
    },
    selectablese(row, index) {
      if (row.hasRole == 1) {
        return false; // 禁用
      } else {
        return true; //不禁用
      }
    },
    getRowKeysSe(row) {
      return row.uuid;
    },
    // 多选框选中数据
    handleSelectionChangeS(selection) {
      this.idss = selection.map(item => item.uuid)
      this.singles = selection.length!=1
      this.multiples = !selection.length
    },
    selectables(row, index) {
      if (row.hasRole == 1) {
        return false; // 禁用
      } else {
        return true; //不禁用
      }
    },
    getRowKeysS(row) {
      return row.uuid;
    },
    handleView(row){
        this.roleDetail = row;
        this.open = true;
        this.title = "角色数据权限详情";
        this.roleAuth = false;
        this.activeNameView = "vfirst";
        let query = {
            roleIds: row.roleId
        }
        getBindRoleProjectList(query).then(res => {
            this.projectList = [];
            this.projectList = res.data;
            var that = this;
            that.$nextTick(() => {
            this.projectList.forEach((v, i) => {
                if (v.hasRole == 1) {
                    this.$refs.tablep1.toggleRowSelection(v, true);
                }
            });
            });
        })
    },
    handleRoleDatasets(row){
        this.roleDetail = row;
        this.open = true;
        this.title = "角色数据权限分配";
        this.roleAuth = true;
        this.activeNameAuth = "afirst";
        // 获取角色项目数据
        let query = {
            roleIds: row.roleId
        }
        getRoleProjectList(query).then(res => {
            this.projectList = [];
            this.projectList = res.data;
            var that = this;
            that.$nextTick(() => {
            this.projectList.forEach((v, i) => {
                if (v.hasRole == 1) {
                    this.$refs.tablep.toggleRowSelection(v, true);
                }
            });
            });
        })
    },
    saveRoleProjects(){
        if(this.idsp.length>0){
            let data = {
                ids: this.idsp.join(","),
                roleIds: this.roleDetail.roleId
            }
            saveRoleProjects(data).then(res =>{
                this.$modal.msgSuccess("保存成功");
                // this.open = false;
                let query = {
                    roleIds: this.roleDetail.roleId
                }
                getRoleProjectList(query).then(res => {
                    this.projectList = [];
                    this.projectList = res.data;
                    var that = this;
                    that.$nextTick(() => {
                    this.projectList.forEach((v, i) => {
                        if (v.hasRole == 1) {
                            this.$refs.tablep.toggleRowSelection(v, true);
                        }
                    });
                    });
                })
            })
        }else{
            this.$message.error('请勾选项目！')
        }
    },
    unhookRoleProject(row){
        let data = {
            uuid: row.uuid,
            roleIds: this.roleDetail.roleId
        }
        unhookRoleProject(data).then(res=>{
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleProjectList(query).then(res => {
                this.projectList = [];
                this.projectList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.projectList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablep.toggleRowSelection(v, true);
                    }
                });
                });
            })
        })
    },
    saveRoleEngineer(){
        if(this.idse.length>0){
            let data = {
                ids: this.idse.join(","),
                roleIds: this.roleDetail.roleId
            }
            saveRoleEngineer(data).then(res =>{
                this.$modal.msgSuccess("保存成功");
                // this.open = false;
                let query = {
                    roleIds: this.roleDetail.roleId
                }
                getRoleEngineerList(query).then(res => {
                    this.engineerList = [];
                    this.engineerList = res.data;
                    var that = this;
                    that.$nextTick(() => {
                    this.engineerList.forEach((v, i) => {
                        if (v.hasRole == 1) {
                            this.$refs.tablee.toggleRowSelection(v, true);
                        }
                    });
                    });
                })
            })
        }else{
            this.$message.error('请勾选工程！')
        }
    },
    unhookRoleEngineer(row){
        let data = {
            uuid: row.uuid,
            roleIds: this.roleDetail.roleId
        }
        unhookRoleEngineer(data).then(res=>{
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleEngineerList(query).then(res => {
                this.engineerList = [];
                this.engineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.engineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablee.toggleRowSelection(v, true);
                    }
                });
                });
            })
        })
    },
    saveRoleSubEngineer(){
        if(this.idsse.length>0){
            let data = {
                ids: this.idsse.join(","),
                roleIds: this.roleDetail.roleId
            }
            saveRoleSubEngineer(data).then(res =>{
                this.$modal.msgSuccess("保存成功");
                // this.open = false;
                let query = {
                    roleIds: this.roleDetail.roleId
                }
                getRoleSubEngineerList(query).then(res => {
                    this.subEngineerList = [];
                    this.subEngineerList = res.data;
                    var that = this;
                    that.$nextTick(() => {
                    this.subEngineerList.forEach((v, i) => {
                        if (v.hasRole == 1) {
                            this.$refs.tablese.toggleRowSelection(v, true);
                        }
                    });
                    });
                })
            })
        }else{
            this.$message.error('请勾选工程！')
        }
    },
    unhookRoleSubEngineer(row){
        let data = {
            uuid: row.uuid,
            roleIds: this.roleDetail.roleId
        }
        unhookRoleSubEngineer(data).then(res=>{
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleSubEngineerList(query).then(res => {
                this.subEngineerList = [];
                this.subEngineerList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.subEngineerList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tablese.toggleRowSelection(v, true);
                    }
                });
                });
            })
        })
    },
    saveRoleStation(){
        if(this.idss.length>0){
            let data = {
                ids: this.idss.join(","),
                roleIds: this.roleDetail.roleId
            }
            saveRoleStation(data).then(res =>{
                this.$modal.msgSuccess("保存成功");
                // this.open = false;
                let query = {
                    roleIds: this.roleDetail.roleId
                }
                getRoleStationList(query).then(res => {
                    this.stationList = [];
                    this.stationList = res.data;
                    var that = this;
                    that.$nextTick(() => {
                    this.stationList.forEach((v, i) => {
                        if (v.hasRole == 1) {
                            this.$refs.tables.toggleRowSelection(v, true);
                        }
                    });
                    });
                })
            })
        }else{
            this.$message.error('请勾选工程！')
        }
    },
    unhookRoleStation(row){
        let data = {
            uuid: row.uuid,
            roleIds: this.roleDetail.roleId
        }
        unhookRoleStation(data).then(res=>{
            let query = {
                roleIds: this.roleDetail.roleId
            }
            getRoleStationList(query).then(res => {
                this.stationList = [];
                this.stationList = res.data;
                var that = this;
                that.$nextTick(() => {
                this.stationList.forEach((v, i) => {
                    if (v.hasRole == 1) {
                        this.$refs.tables.toggleRowSelection(v, true);
                    }
                });
                });
            })
        })
    },
  }
};
</script>